const { findUser, list, addUser, findUserById } = require('./data/user.js')
const { setLoginUser } = require('./data/login')
const base = '/userserver'
const sha1 = require('js-sha1')
const Mock = require('mockjs')

module.exports = [
  // user login
  {
    url: `${base}/users/token`,
    type: 'post',
    response: config => {
      const { phoneNumber, password } = config.body
      const u = findUser(phoneNumber)
      const token = {}

      if (u && u.password === password) {
        setLoginUser(u)
        token.access_token = phoneNumber
        token.expires_in = 15 * 60
        token.refresh_token = phoneNumber
        return { success: true, data: token }
      } else {
        return { success: false, message: '账号或密码错误,登录失败' }
      }
    }
  }, {
    url: `${base}/users/info`,
    type: 'get',
    response: config => {
      const token = config.headers.authorization.substr('Bearer '.length)
      let data = null
      const u = findUser(token)
      if (u) {
        data = JSON.parse(JSON.stringify(u))
        data.password = undefined
      }
      if (data) {
        return { success: true, message: '请求成功', data }
      } else {
        return { success: false, message: '账号或密码错误' }
      }
    }
  }, {
    url: `${base}/users/total`,
    type: 'get',
    response: config => {
      return { success: true, data: Mock.Random.integer(800, 1000) }
    }
  }, {
    url: `${base}/users/todayActiveUser`,
    type: 'get',
    response: config => {
      const arr = []
      for (let i = 0; i <= new Date().getHours(); ++i) {
        arr.push({ hour: i < 10 ? '0' + i : i + '', count: Mock.Random.integer(1, 400) })
      }
      return { success: true, data: arr }
    }
  }, {
    url: `${base}/tokens/refresh`,
    type: 'post',
    response: config => {
      const refreshToken = config.body
      const u = findUser(refreshToken)
      const token = {}

      if (u) {
        setLoginUser(u)
        token.access_token = refreshToken
        token.expires_in = 15 * 60
        token.refresh_token = refreshToken
        return { success: true, data: token }
      } else {
        return { success: false, message: '刷新令牌异常' }
      }
    }
  }, {
    url: `${base}/users`,
    type: 'get',
    response: config => {
      const { page, limit, phoneNumber, orgUnitId, role } = config.query
      return list(page, limit, phoneNumber, orgUnitId, role, null, null)
    }
  }, {
    url: `${base}/users`,
    type: 'post',
    response: config => {
      const { phoneNumber, role, orgUnitId } = config.body
      addUser(phoneNumber, role, orgUnitId)
      return { success: true, message: '保存成功' }
    }
  }, {
    url: `${base}/users/account`,
    type: 'put',
    response: config => {
      const { sourcePassword, newUsername, phoneCode } = config.body
      if (phoneCode !== '121345') {
        return { success: false, message: '手机验证码错误,测试只能是121345' }
      }
      const token = config.headers.authorization.substr('Bearer '.length)
      const user = findUser(token)
      if (user) {
        if (user.password === sourcePassword) {
          user.phoneNumber = newUsername
          return { success: true }
        } else {
          return { success: false, message: '源密码错误' }
        }
      } else {
        return { success: false, message: '用户不存在' }
      }
    }
  }, {
    url: `${base}/users/password`,
    type: 'put',
    response: config => {
      const { sourcePassword, newPassword } = config.body
      const token = config.headers.authorization.substr('Bearer '.length)
      const user = findUser(token)
      if (user) {
        if (user.password === sourcePassword) {
          user.password = newPassword
          return { success: true }
        } else {
          return { success: false, message: '源密码错误' }
        }
      } else {
        return { success: false, message: '用户不存在' }
      }
    }
  }, {
    url: `${base}/users/nickname`,
    type: 'put',
    response: config => {
      const { nickname } = config.body
      console.log(nickname)
      const token = config.headers.authorization.substr('Bearer '.length)
      const user = findUser(token)
      if (user) {
        user.nickname = nickname
        return { success: true }
      } else {
        return { success: false, message: '用户不存在' }
      }
    }
  }, {
    url: `${base}/users/[1-9][0-9]*/enable`,
    type: 'put',
    response: config => {
      const id = config.url.match(/[1-9][0-9]*/)
      const user = findUserById(id)
      if (user) {
        user.disabled = 0
        return { success: true }
      } else {
        return { success: false, message: '用户不存在' }
      }
    }
  }, {
    url: `${base}/users/[1-9][0-9]*/disable`,
    type: 'put',
    response: config => {
      const id = config.url.match(/[1-9][0-9]*/)
      const user = findUserById(id)
      if (user) {
        user.disabled = 1
        return { success: true }
      } else {
        return { success: false, message: '用户不存在' }
      }
    }
  }, {
    url: `${base}/users/[1-9][0-9]*/defaultPwd`,
    type: 'put',
    response: config => {
      const id = config.url.match(/[1-9][0-9]*/)
      const user = findUserById(id)
      if (user) {
        user.password = sha1('88888888')
        return { success: true }
      } else {
        return { success: false, message: '用户不存在' }
      }
    }
  }, {
    url: `${base}/users/[1-9][0-9]*`,
    type: 'put',
    response: config => {
      const id = config.url.match(/[1-9][0-9]*/)
      const user = findUserById(id)
      if (user) {
        const { phoneNumber, orgUnitId } = config.body
        user.phoneNumber = phoneNumber
        user.orgUnitId = orgUnitId
        return { success: true }
      } else {
        return { success: false, message: '用户不存在' }
      }
    }
  }
]
